package ee.android.kinokavad.fragments;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;

import ee.android.kinokavad.App;
import ee.android.kinokavad.R;
import ee.android.kinokavad.adapters.CinemasAdapter;
import ee.android.kinokavad.data.Cinema;

public class CinemasFragment extends SherlockFragment{

	private static final String TAG = CinemasFragment.class.getSimpleName();
	private CinemasAdapter mAdapter;
	private App kinokavad ;
	private List<Boolean> selectedCheck;

	public static Fragment newInstance(Bundle args) {
		Fragment f = new CinemasFragment();
		f.setArguments(args);
		return f;
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		return inflater.inflate(R.layout.cinemas_fragment, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initViews();
		getSherlockActivity().getSupportActionBar().setSubtitle("Kinod");

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	private void initViews() {

		kinokavad = (App) getActivity().getApplicationContext();
		kinokavad.getDataController().open();
		List<Cinema> cinemas = kinokavad.getDataController().loadAllCinemasFromDatabase();
		for (Cinema cinema : cinemas) {
			if (cinema.getName().contains("Solaris")) {
				cinema.setIcon(R.drawable.cinema_solaris);
				continue;
			}
			if (cinema.getName().contains("Mai")) {
				cinema.setIcon(R.drawable.cinema_mai);
				continue;
			}
			if (cinema.getName().contains("Cinamon")) {
				cinema.setIcon(R.drawable.cinema_cinamon);
				continue;
			}
			cinema.setIcon(R.drawable.cinema_forum);
		}
		kinokavad.getDataController().closeRequest();

		selectedCheck = new ArrayList<Boolean>();
		for (Cinema cinema : cinemas) {
			selectedCheck.add(cinema.isSelected());
		}

		ExpandableListView list = (ExpandableListView) getView().findViewById(R.id.cinemasList);
		mAdapter = new CinemasAdapter(getActivity(), cinemas);
		list.setAdapter(mAdapter);

	}


	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.cinemas_menu, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}


	@Override
	public void onStop() {
		super.onStop();

		boolean noneSelected = true;
		kinokavad.getDataController().begin();
		for (int i = 0; i < mAdapter.getGroupCount(); i++) {
			Cinema cinema = (Cinema) mAdapter.getChild(i, 0);
			if (selectedCheck.get(i) != cinema.isSelected()) {
				kinokavad.getDataController().updateCinema(cinema);
			}
			if (cinema.isSelected()) noneSelected = false;
		}

		if (noneSelected && mAdapter.getGroupCount() > 0) {
			Cinema cinema = (Cinema) mAdapter.getChild(0, 0);
			cinema.setSelected(true);
			kinokavad.getDataController().updateCinema(cinema);
		}

		kinokavad.getDataController().success();
		kinokavad.getDataController().end();

	}

}
